package com.example.nexttome_mobile;

import java.net.URLEncoder;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
	private ProgressBar pb;
	private EditText text;
	private RadioGroup rg;
	private RadioButton r1;
	private RadioButton r2;
	
	public static Activity ac;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ac = this;
		
		TextView textView = (TextView)findViewById(R.id.textView1);
		textView.setVisibility(View.GONE);
		
		pb = (ProgressBar) findViewById(R.id.progressBar1);
		pb.setVisibility(View.GONE);
		
		//
		text = (EditText) findViewById(R.id.editText1);
		text.setText("");
		text.setTextColor(Color.parseColor("#00ff00"));
		text.setEnabled(false);
		
		rg = (RadioGroup) findViewById(R.id.radioGroup1);
		r1 = (RadioButton) findViewById(R.id.radio0);
		r2 = (RadioButton) findViewById(R.id.radio1);
		
		r1.setChecked(true);
		
	    rg.setOnCheckedChangeListener(new OnCheckedChangeListener() 
	    {
	        public void onCheckedChanged(RadioGroup group, int checkedId) {
	        	if(r1.isChecked()){
	        		text.setEnabled(false);
	        	}
	        	else if(r2.isChecked()){
	        		text.setEnabled(true);
	        	}
	        }
	    });
		
		final Button button = (Button) findViewById(R.id.btn_buscar);
		button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	String address = text.getText().toString();
            	if(validateAdress(address)){
            	  String str_formatted = prepareString(address); 
            	  searchLocation(str_formatted);
            	}else{
            		showError();
            	}
            }
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
		
	
	public void searchLocation(String address){
			pb.setVisibility(View.VISIBLE);
	    	TextView textView = (TextView)findViewById(R.id.textView1);
	    	new AsyncGET(MainActivity.this, address).execute(textView);
	}
	
	public boolean validateAdress(String address){
		String[] address_array = address.split("\\s+");
		int len = address_array.length;
		if(isNumeric(address_array[len-1])){
			for(int i=0; i<len-1; i++){
				if(!isAlphaNumeric(address_array[i])){
					return false;
				}
			}
		}else{
			return false;
		}
		return true;
	}
	
	public static boolean isNumeric(String str)  
	{  
	  try  
	  {  
	    @SuppressWarnings("unused")
		double d = Double.parseDouble(str);  
	  }  
	  catch(NumberFormatException nfe)  
	  {  
	    return false;  
	  }  
	  return true;  
	}
	
	public boolean isAlphaNumeric(String word) {
	    return word.matches("[a-zA-Z0-9]+");
	}
	
	public String prepareString(String str){
		str = str.trim();
		@SuppressWarnings("deprecation")
		String newString = URLEncoder.encode(str);
		return newString;
	}
	
	public void showError(){
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
			// set title
		alertDialogBuilder.setTitle("Error");
			// set dialog message
		alertDialogBuilder
			.setMessage("Direccion incorrecta, por favor ingrese nuevamente la direccion. \nEj: Montevideo 1000")
			.setCancelable(false)
			.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,int id) {
					text.setText("");
				}
			  });
			// create alert dialog
			AlertDialog alertDialog = alertDialogBuilder.create();
			// show it
			alertDialog.show();
	}
	
		
}
